/**
 * 给定了若干条件，求满足条件的字符串数量
 * 注意到变量只有5个，因此最多只有1E5的可能性
 * 字符串长度也只有9，所以加上字符串的判断循环次数也在1E6
 */
#include <bits/stdc++.h>
using namespace std;

using llt = long long;
using vi = vector<int>;
using vvi = vector<vi>;

llt const MOD = 1E9 + 7;

int N;
string X, Y;

bool check(const string & s){
    if('0' == s[0] and N > 1) return false;
    int t = 0;
    for(int i=max(0,N-3);i<N;++i)t=t*10+s[i]-'0';
    if(t%8)return false;
    return s <= Y;
}

llt proc(){
    set<string> s;
    for(int a='0';a<='9';++a)for(int b='0';b<='9';++b){
        if(a == b) continue;
        for(int c='0';c<='9';++c){
            if(c == a or c == b) continue;
            for(int d='0';d<='9';++d){
                if(d == c or d == a or d == b) continue;
                for(int t='0';t<='9';++t){
                    string x(X.begin(), X.end());
                    for(auto & ch : x){
                        if(ch == 'a') ch = a;
                        else if(ch == 'b') ch = b;
                        else if(ch == 'c') ch = c;
                        else if(ch == 'd') ch = d;
                        else if(ch == '_') ch = t;
                        else if(isdigit(ch)) ;
                        else assert(0);
                    }
                    if(check(x)) s.insert(x); 
                }
            }
        }
    }
    return s.size();    
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(0);
    int nofkase = 1;
    cin >> nofkase;
    while(nofkase--){
        cin >> N >> X >> Y;
		cout << proc() << "\n";
    }
    return 0;
}